package com.zut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zut.common.BaseContext;
import com.zut.entity.AddressBook;
import com.zut.mapper.AddressBookMapper;
import com.zut.service.AddressBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Override
    @Transactional
    public void setDefault(AddressBook addressBook) {
        AddressBook oldDefault=new AddressBook();
        oldDefault.setIsDefault(0);
        addressBook.setIsDefault(1);
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        this.update(oldDefault, queryWrapper);
        this.updateById(addressBook);
    }
}
